package com.yyw.gulimall.member.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Classname MyThreadConfig
 * @Description 自定义线程池
 * @Date 2020/12/9 14:51
 * @Created yyw
 */
@Configuration
public class MyThreadConfig {

    /**
     * 自定义线程池
     * ThreadExecutorProperties 因为我们把他加入到容器，所以可以直接注入，读取配置
     *
     * @param properties
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadExecutorProperties properties) {
        return new ThreadPoolExecutor(properties.getCore(),
                properties.getMaxSize(),
                properties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(properties.getQueueSize()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

}
